package com.atmilan.quartz.sync.pull.europe.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atmilan.quartz.model.entity.europe.EuropeProductSku;
import com.atmilan.quartz.model.entity.europe.EuropeProductSkuMerged;
import com.atmilan.quartz.sync.pull.europe.mapper.EuropeProductSkuMergedMapper;
import com.atmilan.quartz.sync.pull.europe.service.IEuropeProductSkuMergedService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class EuropeProductSkuMergedServiceImpl extends ServiceImpl<EuropeProductSkuMergedMapper, EuropeProductSkuMerged> implements IEuropeProductSkuMergedService {

    @Autowired
    EuropeProductSkuMergedMapper europeProductSkuMergedMapper;
    @Override
    public List<String> findAllSkuMerged(){
        List<Object> list = europeProductSkuMergedMapper.selectObjs(new QueryWrapper<EuropeProductSkuMerged>().select(" concat(goods_id,'-',color,'-',size) "));
        return list == null? new ArrayList<String>():list.stream().map(o->o.toString()).collect(Collectors.toList());
    }

    @Override
    public EuropeProductSkuMerged toMergedModel(EuropeProductSku sku) {
        if ( sku == null ) {
            return null;
        }
        EuropeProductSkuMerged europeProductSkuMerged = new EuropeProductSkuMerged();
        europeProductSkuMerged.setSkuId( sku.getId() );
        europeProductSkuMerged.setGoodsId( sku.getGoodsId() + "" );
        europeProductSkuMerged.setStock( sku.getQty());
        europeProductSkuMerged.setColor( sku.getColor() );
        europeProductSkuMerged.setSize( sku.getSize() );
        europeProductSkuMerged.setQihuo( null );
        europeProductSkuMerged.setUpdateFlag( sku.getUpdateFlag() );
        europeProductSkuMerged.setCreateTime( sku.getNewCreatedTime() );
        europeProductSkuMerged.setEditTime( sku.getNewModifyTime() );
        europeProductSkuMerged.setSuk( sku.getSuk() );
        return europeProductSkuMerged;
    }

    @Override
    public List<EuropeProductSkuMerged> getByGoodsId(String goodsId) {
        QueryWrapper<EuropeProductSkuMerged> qw = new QueryWrapper<>();
        qw.lambda().eq(EuropeProductSkuMerged::getGoodsId,goodsId);
        qw.lambda().gt(EuropeProductSkuMerged::getStock,0);
        return this.list(qw);
    }

    @Override
    public void deleteAll() {
        europeProductSkuMergedMapper.deleteAll();
    }
    
    @Override
    public void copyAll() {
        europeProductSkuMergedMapper.deleteProductSkuMergedBack();
        europeProductSkuMergedMapper.copyProductSkuMergedBack();
    }
    
}
